package com.toune.dltools.view.title

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.toune.dltools.R
import com.toune.dltools.ui.DLActivityConfig
import kotlinx.android.synthetic.main.layout_base_activity_title.view.*

/**
 * @Author Dong Lei
 * @Date 2020/12/25 0025-下午 16:48
 * @Info 描述：
 */
class DLBaseTitleView(
    context: Context,
    mRootViewGroup: ViewGroup,
    titleString: String?,
    isHideBack: Boolean
) : DLCustomTitleView(titleString) {

    private var mRootTitileView: ViewGroup? = null
    private val isHideBack: Boolean = isHideBack
    private val WM = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT)
    fun addRightView(view: View):View {
        WM.gravity = Gravity.CENTER_VERTICAL
        mRootTitileView!!.rightLv.addView(
            view,WM
        )
        return view
    }

    fun addRightTv(str: String):View {
        WM.gravity = Gravity.CENTER_VERTICAL
        var tv = TextView(cxt)
        tv.text = str
        tv.textSize = 16f
        tv.gravity = Gravity.CENTER
        tv.setTextColor(DLActivityConfig.defTitleTextColor)
        mRootTitileView!!.rightLv.addView(
            tv,WM
        )
        return tv
    }

    fun addRightIv(id: Int) :View{
        WM.gravity = Gravity.CENTER_VERTICAL
        var iv = ImageView(cxt)
        iv.setImageResource(id)
        mRootTitileView!!.rightLv.addView(
            iv,WM
        )
        return iv
    }

    fun removeRightView(){
        mRootTitileView!!.rightLv.removeAllViews()
    }

    var cxt: Context = context
    var viewGroup = mRootViewGroup
    override fun initView(context: Context, mRootViewGroup: ViewGroup): ViewGroup {
        mRootTitileView =
            View.inflate(cxt, R.layout.layout_base_activity_title, mRootViewGroup) as ViewGroup
        //标题  ：布局文件中引入lyout_title
        mRootTitileView!!.titleRv.setBackgroundColor(DLActivityConfig.defTitleBgColor)
        mRootTitileView!!.titleTv.setTextColor(DLActivityConfig.defTitleTextColor)
        mRootTitileView!!.titleTv.textSize = DLActivityConfig.defTitleTextSize
        if (DLActivityConfig.defTitleBackIvImgRes != null) {
            mRootTitileView!!.backIv.setImageResource(DLActivityConfig.defTitleBackIvImgRes!!)
        }
        if (DLActivityConfig.defTitleBackStr.isNullOrBlank()) {
            mRootTitileView!!.backTv.visibility = View.GONE
        } else {
            mRootTitileView!!.backTv.visibility = View.VISIBLE
            mRootTitileView!!.backTv.text = DLActivityConfig.defTitleBackStr
            mRootTitileView!!.backTv.setTextColor(DLActivityConfig.defTitleTextColor)
        }
        mRootTitileView!!.backIv.setColorFilter(DLActivityConfig.defTitleBackIvImgColor!!)
        mRootTitileView!!.titleTv.gravity = DLActivityConfig.defTitleGravity
        mRootTitileView!!.backLv.setOnClickListener(View.OnClickListener { onBackIv((context as Activity)) })
        hideBack(isHideBack)
        mRootTitileView!!.titleTv.text = getTitleStr()
        return mRootTitileView!!
    }

    override fun setTitleStr(string: String?) {
        super.setTitleStr(string)
        mRootTitileView!!.titleTv.text = getTitleStr()
    }

    private fun hideBack(hideBack: Boolean) {
        if (hideBack) {
            mRootTitileView!!.backLv.visibility = View.GONE
        } else {
            mRootTitileView!!.backLv.visibility = View.VISIBLE
        }
    }
}